using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {
	
	public static int score = 0;
	public static int lives = 3;
	public static int tempScore = 0;
	
	// Use this for initialization
	void Start () {
	
		Time.timeScale = 1;
		
	}
	
	// Update is called once per frame
	void Update () {
	
		if(tempScore > 1045) tempScore -= 1000;
		
		if(lives <= 0) GameOver();
	}
	
	void OnGUI()
	{
	
		GUI.Box(new Rect(Screen.width - 205, 5, 200, 20), "Score: " + score);
	}
	
	void GameOver()
	{
		int delay = Time.frameCount;
		Time.timeScale = 0;
		AddScore(score);
		
		if(delay >= 360)
		{
			Application.LoadLevel("MainMenu");
		}
	}
	
	void AddScore(int score)
	{
		int myScores;
		int tempScore;
		string myName;
		string tempName;
		for (int i = 0; i < 10; i++)
		{
  			myScores = PlayerPrefs.GetInt("Scores"+i);
			myName = PlayerPrefs.GetString("Names"+i);
			if(score > myScores)
			{
				tempScore = myScores;
				myScores = score;
				score = tempScore;
				tempName = myName;
				myName = Difficulty.name;
				Difficulty.name = tempName;
			}
			PlayerPrefs.SetInt("Scores"+i, myScores);
			PlayerPrefs.SetString("Names"+i, myName);
			
		}
		
		PlayerPrefs.Save();
	}
}
